// @title LLM网关
// @version 1.0
// @description 这是一个使用Gin框架构建的K8S服务API文档
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8080
// @BasePath /
// @schemes http

package main

import (
	"log"
	"path/filepath"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"

	handlers "k8s-service/controller"
	_ "k8s-service/docs" // docs is generated by Swag CLI, you have to import it.
	"k8s-service/service"
)

func main() {
	// 初始化模型配置服务
	dbPath := filepath.Join("data", "sqlite")
	if err := handlers.InitModelConfigService(dbPath); err != nil {
		log.Fatal("初始化模型配置服务失败:", err)
	}
	log.Println("模型配置服务初始化成功")

	// 初始化代理服务
	modelConfigService, err := service.NewModelConfigService(dbPath)
	if err != nil {
		log.Fatal("创建模型配置服务失败:", err)
	}
	proxyService := service.NewProxyService(modelConfigService)
	handlers.SetProxyService(proxyService)
	log.Println("代理服务初始化成功")

	// 创建Gin路由器
	r := gin.Default()

	// 添加中间件
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	// 静态文件服务
	r.Static("/static", "./static")

	// Web管理界面路由
	admin := r.Group("/admin")
	{
		admin.GET("", handlers.AdminIndex)
		admin.GET("/", handlers.AdminIndex)
		admin.GET("/create", handlers.AdminCreate)
		admin.POST("/store", handlers.AdminStore)
		admin.GET("/view/:id", handlers.AdminView)
		admin.GET("/edit/:id", handlers.AdminEdit)
		admin.POST("/update/:id", handlers.AdminUpdate)
	}

	// 根路径模型配置路由（用于前端兼容）
	r.POST("/model-config", handlers.CreateModelConfig)
	r.GET("/model-config", handlers.ListModelConfigs)
	r.GET("/model-config/enabled", handlers.GetEnabledModelConfigs)
	r.GET("/model-config/:id", handlers.GetModelConfig)
	r.PUT("/model-config/:id", handlers.UpdateModelConfig)
	r.DELETE("/model-config/:id", handlers.DeleteModelConfig)
	r.POST("/model-config/:id/toggle", handlers.ToggleModelConfigStatus)

	// OpenAI 兼容的代理接口
	v1 := r.Group("/v1")
	{
		// 聊天完成接口
		v1.POST("/chat/completions", handlers.ChatCompletions)
		// 嵌入接口
		v1.POST("/embeddings", handlers.Embeddings)
		// 重排序接口
		v1.POST("/rerank", handlers.Rerank)
	}

	// API路由组
	api := r.Group("/api/v1")
	{
		// 模型配置相关路由
		api.POST("/model-config", handlers.CreateModelConfig)
		api.GET("/model-config", handlers.ListModelConfigs)
		api.GET("/model-config/enabled", handlers.GetEnabledModelConfigs)
		api.GET("/model-config/:id", handlers.GetModelConfig)
		api.PUT("/model-config/:id", handlers.UpdateModelConfig)
		api.DELETE("/model-config/:id", handlers.DeleteModelConfig)
		api.POST("/model-config/:id/toggle", handlers.ToggleModelConfigStatus)
	}

	// Swagger文档路由
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 启动信息
	log.Println("服务器启动中...")
	log.Println("Web管理界面: http://localhost:8080/admin")
	log.Println("API文档地址: http://localhost:8080/swagger/index.html")
	log.Println("模型配置管理地址: http://localhost:8080/api/v1/model-config")
	log.Println("OpenAI 兼容接口:")
	log.Println("  - 聊天完成: http://localhost:8080/v1/chat/completions")
	log.Println("  - 嵌入模型: http://localhost:8080/v1/embeddings")
	log.Println("  - 重排序模型: http://localhost:8080/v1/rerank")

	// 启动服务器，监听8080端口
	if err := r.Run(":8080"); err != nil {
		log.Fatal("服务器启动失败:", err)
	}
}
